<style>
.today { text-align: center; background: #fff; border: 1px solid #999; padding: 20px; }
.today div.t_title { font-family: Lucida Sans Unicode !important; font-weight: bold; font-size: 20px !important; }
.today div.t_order { font-family: Lucida Sans Unicode !important; font-weight: bold; font-size: 24px !important; color: #ff8000; margin: 5px 0; }
.today div.t_income { font-family: Verdana !important; font-weight: bold; font-size: 16px !important; color: #e7806f; }
.today div.t_unpaid { font-family: Lucida Sans Unicode !important; font-size: 13px !important; color: #6f6f6f; }
.pure_form_extra { background: #fff; padding: 10px; border: 1px solid #aaa; border-radius: 8px; font-size: 12px; }
</style>

<?php
$rslt = $orm->get('pb_ecom_currency', NULL, NULL, NULL, NULL, NULL)->d;
$currs = array();
foreach ($rslt as $itm) {
  if ($itm->is_default == 1) {
    $c1 = $itm;
  }
  $currs[$itm->title] = $itm->rate;
}

$opts = new stdClass();
$opts->cd = 'date > :v1 AND is_cancel = :v2';
$opts->cdv = array(':v1' => date('Y-m-d', time() - (86400 * 14)), ':v2' => 0);
$opts->sel = 't.date, t.curr, t.total, t.delivery_subtotal, t.is_paid';
$rslt = $orm->get('pb_ecom_order', NULL, NULL, NULL, NULL, $opts)->d;
$last14 = array();
$max = 0;
for ($i = time(), $il = time() - (86400 * 14); $i > $il; $i -= 86400) {
  $d = date('Y-m-d', $i);
  $count = 0; 
  $amount = 0; 
  $count_un = 0; 
  $amount_un = 0;
  foreach ($rslt as $itm) {
    if ($itm->date == $d) {
      $count++;
      $amount += ($currs[$itm->curr] / $c1->rate) * ($itm->total + $itm->delivery_subtotal);
      if ($itm->is_paid == 0) {
        $count_un++;
        $amount_un += ($currs[$itm->curr] / $c1->rate) * ($itm->total + $itm->delivery_subtotal);
      }
    }
  }
  $max = max($max, $amount);
  $last14[] = array($d, $amount, '$' . $amount);
  
  if ($i == time()) {
    $today_orders = $count;
    $today_income  = $amount;
    $today_unpaid_orders = $count_un;
    $today_unpaid_income = $amount_un;
  }
}

$opts = new stdClass();
$opts->cd = 't.is_paid = :v1 AND ship_date = :v2 AND t.is_cancel = :v0';
$opts->cdv = array(':v1' => 1, ':v2' => '0000-00-00', ':v0' => 0);
$opts->sel = 'COUNT(t.id) AS __total';
$od_ship = $orm->get('pb_ecom_order', NULL, NULL, NULL, NULL, $opts)->d[0]->__total;
$opts = new stdClass();
$opts->cd = 't.is_paid = :v1 AND t.is_cancel = :v0';
$opts->cdv = array(':v1' => 0, ':v0' => 0);
$opts->sel = 'COUNT(t.id) AS __total';
$od_unpaid = $orm->get('pb_ecom_order', NULL, NULL, NULL, NULL, $opts)->d[0]->__total;
$opts = new stdClass();
$opts->cd = 't.ship_date >= :v1 AND t.ship_date != :v2 AND t.is_cancel = :v0';
$opts->cdv = array(':v1' => date('Y-m-d', time() - (86400 * 10)), ':v2' => '0000-00-00', ':v0' => 0);
$opts->sel = 'COUNT(t.id) AS __total';
$od_ship10 = $orm->get('pb_ecom_order', NULL, NULL, NULL, NULL, $opts)->d[0]->__total;

$opts = new stdClass();
$opts->cd = 't.status = :v1';
$opts->cdv = array(':v1' => 1);
$opts->sel = 'COUNT(t.id) AS __total';
$prod_ava = $orm->get('pb_ecom_product', NULL, NULL, NULL, NULL, $opts)->d[0]->__total;
$opts = new stdClass();
$opts->cd = 't.status = :v1';
$opts->cdv = array(':v1' => 0);
$opts->sel = 'COUNT(t.id) AS __total';
$prod_unava = $orm->get('pb_ecom_product', NULL, NULL, NULL, NULL, $opts)->d[0]->__total;
$opts = new stdClass();
$opts->cd = 't.alert__ = :v1 AND t.status = :v2';
$opts->cdv = array(':v1' => 1, ':v2' => 1);
$opts->sel = 'COUNT(t.id) AS __total';
$prod_out = $orm->get('pb_ecom_product', NULL, NULL, NULL, NULL, $opts)->d[0]->__total;

$opts = new stdClass();
$opts->cd = 't.status = :v1';
$opts->cdv = array(':v1' => 1);
$opts->sel = 'COUNT(t.id) AS __total';
$cus_curr = $orm->get('pb_ecom_customer', NULL, NULL, NULL, NULL, $opts)->d[0]->__total;
$opts = new stdClass();
$opts->cd = 't.status = :v1';
$opts->cdv = array(':v1' => 0);
$opts->sel = 'COUNT(t.id) AS __total';
$cus_inact = $orm->get('pb_ecom_customer', NULL, NULL, NULL, NULL, $opts)->d[0]->__total;
$opts = new stdClass();
$opts->cd = 't.status = :v1';
$opts->cdv = array(':v1' => 2);
$opts->sel = 'COUNT(t.id) AS __total';
$cus_sus = $orm->get('pb_ecom_customer', NULL, NULL, NULL, NULL, $opts)->d[0]->__total;
?>

<table cellpadding=0 cellspacing=0>
  <tr>
    <td style="vertical-align: top;">
      <h3>Orders of last 14 days</h3>
      <div id="chart1" style="height:200px; width:650px;"></div>
    </td>
    <td style="vertical-align: top;">
      <div class="today">
        <div class="t_title">Today's Order</div>
        <div class="t_order"><?php echo $today_orders; ?></div>
        <div class="t_income">$<?php echo $today_income; ?></div>
        <div>&nbsp;</div>
        <div class="t_unpaid"><?php echo $today_unpaid_orders; ?> Unpaid Orders ($<?php echo $today_unpaid_income; ?>)</div>
      </div>
      <div>&nbsp;</div>
      <div class="pure-form pure-form-aligned pure_form_extra" style="padding: 10px; width:350px;">
        <legend>Orders</legend>
        <div class="pure-control-group">
          <label <?php if ($od_ship > 0) echo 'style="color: #ff8000;"'; ?>>Shipping Required</label>
          <a <?php if ($od_ship > 0) echo 'style="color: #ff8000;"'; ?> href="#" onclick="return Url.redirect(__redmd, __sys + '.php?i=' + __adm_od_id + '&search=' + Utils.encodeURI(JSON.stringify({ type: 2, phrase: '' })));"><?php echo $od_ship; ?></a>
        </div>
        <div class="pure-control-group">
          <label <?php if ($od_unpaid > 0) echo 'style="color: #ff8000;"'; ?>>Unpaid Orders</label>
          <a <?php if ($od_unpaid > 0) echo 'style="color: #ff8000;"'; ?> href="#" onclick="return Url.redirect(__redmd, __sys + '.php?i=' + __adm_od_id + '&search=' + Utils.encodeURI(JSON.stringify({ type: 1, phrase: '' })));"><?php echo $od_unpaid; ?></a>
        </div>
        <div class="pure-control-group">
          <label>Shipped in 10 days</label>
          <a href="#" onclick="return Url.redirect(__redmd, __sys + '.php?i=' + __adm_od_id + '&search=' + Utils.encodeURI(JSON.stringify({ type: 3, phrase: '' })));"><?php echo $od_ship10; ?></a>
        </div>
      </div>
      <div>&nbsp;</div>
      <div class="pure-form pure-form-aligned pure_form_extra" style="padding: 10px; width:350px;">
        <legend>Products</legend>
        <div class="pure-control-group">
          <label>Available</label>
          <a href="#" onclick="return Url.redirect(__redmd, __sys + '.php?i=' + __adm_prod_id + '&search=' + Utils.encodeURI(JSON.stringify({ type: 1, phrase: '' })));"><?php echo $prod_ava; ?></a>
        </div>
        <div class="pure-control-group">
          <label>Unavailable</label>
          <a href="#" onclick="return Url.redirect(__redmd, __sys + '.php?i=' + __adm_prod_id + '&search=' + Utils.encodeURI(JSON.stringify({ type: 2, phrase: '' })));"><?php echo $prod_unava; ?></a>
        </div>
        <div class="pure-control-group">
          <label <?php if ($prod_out > 0) echo 'style="color: #ff8000;"'; ?>>Out of stock</label>
          <a <?php if ($prod_out > 0) echo 'style="color: #ff8000;"'; ?> href="#" onclick="return Url.redirect(__redmd, __sys + '.php?i=' + __adm_prod_id + '&search=' + Utils.encodeURI(JSON.stringify({ type: 3, phrase: '' })));"><?php echo $prod_out; ?></a>
        </div>
      </div>
      <div>&nbsp;</div>
      <div class="pure-form pure-form-aligned pure_form_extra" style="padding: 10px; width:350px;">
        <legend>Customers</legend>
        <div class="pure-control-group">
          <label>Current</label>
          <a href="#" onclick="return Url.redirect(__redmd, __sys + '.php?i=' + __adm_cus_id + '&search=' + Utils.encodeURI(JSON.stringify({ type: 2, phrase: '' })));"><?php echo $cus_curr; ?></a>
        </div>
        <div class="pure-control-group">
          <label <?php if ($cus_inact > 0) echo 'style="color: #ff8000;"'; ?>>Inactivated</label>
          <a <?php if ($cus_inact > 0) echo 'style="color: #ff8000;"'; ?> href="#" onclick="return Url.redirect(__redmd, __sys + '.php?i=' + __adm_cus_id + '&search=' + Utils.encodeURI(JSON.stringify({ type: 1, phrase: '' })));"><?php echo $cus_inact; ?></a>
        </div>
        <div class="pure-control-group">
          <label>Suspended</label>
          <a href="#" onclick="return Url.redirect(__redmd, __sys + '.php?i=' + __adm_cus_id + '&search=' + Utils.encodeURI(JSON.stringify({ type: 3, phrase: '' })));"><?php echo $cus_sus; ?></a>
        </div>
      </div>
    </td>
  </tr>
</table>

<script type="text/javascript">
  var __last14 = JSON.parse('<?php echo addslashes(json_encode($last14)); ?>');
  var __id = '<?php echo $app_data->param_id; ?>';
  var __sys = '<?php echo addslashes($app_data->sys->php_name); ?>';
  var __redmd = '<?php echo addslashes($app_data->sys->redmd); ?>';
  var __adm_od_id = '<?php echo ADM_OD_ID; ?>';
  var __adm_prod_id = '<?php echo ADM_PROD_ID; ?>';
  var __adm_cus_id = '<?php echo ADM_CUS_ID; ?>';
  var __max = '<?php echo $max; ?>';
  
  $(function() {
    var plot1 = $.jqplot('chart1', [__last14], {
      seriesDefaults: {
        rendererOptions: {
          smooth: true
        },
        showMarker:false,
        pointLabels: { show:true } 
      },
      axes:{
        xaxis:{
          renderer:$.jqplot.DateAxisRenderer,
          tickOptions:{formatString:'%#d %b'},
          tickInterval:'1 day'
        },
        yaxis: {
          min: 0,
          max: __max * 2,
          tickOptions: {
            prefix: '',
            formatString:'%i',
            showLabel: false
          }
        }
      }
    });
    
  });
</script>